package com.xiaoxx.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxx.annotation.AuthManage;
import com.xiaoxx.annotation.LogRecords;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.common.result.R;
import com.xiaoxx.common.result.RType;
import com.xiaoxx.model.dto.sys.*;
import com.xiaoxx.model.vo.sys.SysUserListVo;
import com.xiaoxx.model.vo.sys.SysUserLoginVo;
import com.xiaoxx.model.vo.sys.SysUserTreeListVo;
import com.xiaoxx.model.vo.sys.SysUserVo;
import com.xiaoxx.service.SysUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author :HX
 * @date : 2023/7/19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/sys/user")
@Api(value = "SysUserController", tags = "用户管理")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 用户注册
     *
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "注册")
    @ApiImplicitParams({@ApiImplicitParam(name = "userRegisterDto", value = "注册vo不能为空", required = true, dataType = "UserRegisterDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @LogRecords(moduleName = "登录模块", content = "用户注册")
    public R register(@Valid @RequestBody UserRegisterDto userRegisterDto) {
        Boolean b = sysUserService.register(userRegisterDto);
        return R.success(b);
    }

    @ApiOperation("用户登录")
    @ApiImplicitParams({@ApiImplicitParam(name = "userLogin", value = "用户登录Dto", required = true, dataType = "UserLoginDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @LogRecords(moduleName = "登录模块", content = "用户登录")
    public R<SysUserLoginVo> login(@Valid @RequestBody UserLoginDto userLogin) {
        SysUserLoginVo b = sysUserService.login(userLogin);
        return R.success(b);
    }

    @ApiOperation("用户登出")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/login/out", method = RequestMethod.GET)
    public R<Boolean> loginOut() {
        return R.success(sysUserService.loginOut());
    }

    @ApiOperation("获取验证码")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/getCode", method = RequestMethod.GET)
    public R<Map<String, String>> getCode() {
        return R.success(sysUserService.getCode());
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "查询用户列表", required = true, dataType = "SysUserQuery", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = SysUserListVo.class, code = 200, message = "响应成功")})
    public R<IPage<SysUserListVo>> list(@RequestBody SysUserQuery query) {
        return R.success(sysUserService.listUser(query));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "添加用户", required = true, dataType = "SysUserDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = String.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "用户模块", content = "添加用户")
    public R<Boolean> add(@Valid @RequestBody SysUserDto dto) {
        if (StringUtils.hasLength(dto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_FALSE);
        }
        return R.success(sysUserService.add(dto));
    }

    @RequestMapping(value = "/upd", method = RequestMethod.PUT)
    @ApiOperation(value = "修改用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "修改用户", required = true, dataType = "SysUserDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = String.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "用户模块", content = "修改用户")
    public R<Boolean> upd(@Valid @RequestBody SysUserDto dto) {
        if (!StringUtils.hasLength(dto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_TRUE);
        }
        return R.success(sysUserService.upd(dto));
    }

    @RequestMapping(value = "/updPassword", method = RequestMethod.PUT)
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "修改密码", required = true, dataType = "SysUserPasswordDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(response = String.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "用户模块", content = "修改密码")
    public R<Boolean> updPassword(@Valid @RequestBody SysUserPasswordDto dto) {
        return R.success(sysUserService.updPassword(dto));
    }

    @RequestMapping(value = "/updDisable", method = RequestMethod.GET)
    @ApiOperation(value = "启用/禁用", notes = "启用/禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "disable", value = "disable", required = true, paramType = "query")
    })
    @LogRecords(moduleName = "用户模块", content = "启用/禁用用户")
    @AuthManage(roles = {"SYS"})
    public R<Boolean> updDisable(@RequestParam String id, @RequestParam Integer disable) {
        return R.success(sysUserService.updDisable(id, disable));
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ApiOperation(value = "ID删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "删除", required = true, dataType = "String", paramType = "query")})
    @ApiResponses(value = {@ApiResponse(response = Boolean.class, code = 200, message = "响应成功")})
    @LogRecords(moduleName = "用户模块", content = "删除用户")
    @AuthManage(roles = {"SYS"})
    public R<Boolean> del(@RequestParam String id) {
        return R.successDelete(sysUserService.del(id));
    }

    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ApiOperation(value = "通过id查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "query", value = "通过id查询", required = true, dataType = "String", paramType = "query")})
    @ApiResponses(value = {@ApiResponse(response = SysUserVo.class, code = 200, message = "响应成功")})
    public R<SysUserVo> findById(@RequestParam String id) {
        return R.success(sysUserService.findById(id));
    }

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户信息")
    @ApiResponses(value = {@ApiResponse(response = SysUserVo.class, code = 200, message = "响应成功")})
    public R<SysUserVo> getUserInfo() {
        return R.success(sysUserService.getUserInfo());
    }

    @RequestMapping(value = "/findUserTree", method = RequestMethod.GET)
    @ApiOperation(value = "通过id查询")
    @ApiResponses(value = {@ApiResponse(response = SysUserTreeListVo.class, code = 200, message = "响应成功")})
    public R<List<SysUserTreeListVo>> findUserTree() {
        return R.success(sysUserService.findUserTree());
    }

    @GetMapping("/getIpInfo")
    @ApiOperation(value = "获取ip访问位置")
    public R<String> getIpInfo() {
        String addressName = sysUserService.getIpInfo();
        return R.success(addressName);
    }

    //    ----------------------gitee--------------------------
    @ApiOperation("获取链接")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/gitee/url", method = RequestMethod.GET)
    @LogRecords(moduleName = "登录模块", content = "获取gitee url链接")
    public R<String> getGiteeUrl(@RequestParam String type) {
        String url = sysUserService.getGiteeUrl(type);
        return R.success(url);
    }

    @ApiOperation("回调")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/gitee/callback", method = RequestMethod.GET)
    @LogRecords(moduleName = "登录模块", content = "gitee登录回调")
    public void callback(@RequestParam String code, HttpServletResponse httpServletResponse) throws IOException {
        String token = sysUserService.callback(code);
        if (StringUtils.hasLength(token)) {
            httpServletResponse.sendRedirect("http://admin.hx.xijia.plus:9091/giteeLogin?token=" + token);
        } else {
            httpServletResponse.sendRedirect("http://admin.hx.xijia.plus:9091/login");
        }
    }

    @ApiOperation("回调")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/user/gitee/callback", method = RequestMethod.GET)
    @LogRecords(moduleName = "登录模块", content = "gitee登录回调")
    public void userCallback(@RequestParam String code, HttpServletResponse httpServletResponse) throws IOException {
        String token = sysUserService.userCallback(code);
        if (StringUtils.hasLength(token)) {
            httpServletResponse.sendRedirect("http://user.hx.xijia.plus/giteeLogin?token=" + token);
        } else {
            httpServletResponse.sendRedirect("http://user.hx.xijia.plus/login");
        }
    }

    @ApiOperation("token登录")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/login/token", method = RequestMethod.GET)
    @LogRecords(moduleName = "登录模块", content = "第三方应用token登录")
    public R<SysUserLoginVo> loginByToken(@RequestParam Integer type) {
        SysUserLoginVo b = sysUserService.loginByToken(type);
        return R.success(b);
    }
}
